package com.rdc.example.service.impl;

import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.CommentDTO;
import com.rdc.example.dao.ArticleDao;
import com.rdc.example.dao.CommentDao;
import com.rdc.example.dao.impl.ArticleDaoImpl;
import com.rdc.example.dao.impl.CommentDaoImpl;
import com.rdc.example.dao.impl.CommentLikeDaoImpl;
import com.rdc.example.dao.impl.UserDaoImpl;
import com.rdc.example.entity.Comment;
import com.rdc.example.entity.User;
import com.rdc.example.service.CommentServial;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author duck
 */
public class CommentServiceImpl implements CommentServial {

    public static void main(String[] args) {
        //List<CommentDTO> listCommentByArticleId(Integer articleId)
        CommentServial commentServial = new CommentServiceImpl();
        CommentDTO commentDTO = new CommentDTO();
        commentDTO.setContent("零八十六");
        commentDTO.setArticleId(1);
        commentDTO.setUserId(1);
        //Result result= commentServial.insertComment(commentDTO);
        //System.out.println(result);
        List<CommentDTO> list= commentServial.listCommentByArticleId(2);
        System.out.println(list);
    }

    /**
     *
     * @param commentDTO
     * @return
     */
    @Override
    public Result insertComment(CommentDTO commentDTO) {
        try {
            CommentDao commentDao= new CommentDaoImpl();
            Comment commentEntity=convertToEntity(commentDTO);
            ArticleDao articleDao= new ArticleDaoImpl();
            boolean registerArticle= articleDao.updateTime(commentDTO.getArticleId());
            boolean registerComment=commentDao.insertComment(commentEntity);
            return registerArticle&&registerComment  ? Result.success("评论成功", null)
                    : Result.fail("评论失败", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *
     * @param commentId
     * @return
     */
    @Override
    public Result deleteComment(Integer commentId) {
        try {
            CommentDao commentDao= new CommentDaoImpl();
            CommentLikeDaoImpl commentLikeDao= new CommentLikeDaoImpl();
            System.out.println(commentId);
            boolean registerComment=commentDao.deleteById(commentId);
            boolean registerCommentLike=commentLikeDao.deleteLikeByCommentId(commentId);
            return registerComment&&registerCommentLike  ? Result.success("删除成功", null)
                    : Result.fail("删除失败", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *
     * @param articleId
     * @return
     */
    @Override
    public  List<CommentDTO> listCommentByArticleId(Integer articleId) {
        List<CommentDTO> commentDTOList = new ArrayList<>();
        try {
            CommentDao commentDao = new CommentDaoImpl();
            List<Comment> commentEntityList=commentDao.listCommentByArticleId(articleId);
            if (commentEntityList.isEmpty()) {
                return commentDTOList;
            }
            return convertAndEnrichComment(commentEntityList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    private List<CommentDTO> convertAndEnrichComment(List<Comment> commentEntityList) {
        List<CommentDTO> dtos = new ArrayList<>();
        Set<Integer> userIds = commentEntityList.stream()
                .map(Comment::getUserId)
                .collect(Collectors.toSet());
        UserDaoImpl userDao = new UserDaoImpl();
        Map<Integer, User> userMap = userDao.getUsersByIds(userIds);
        // 转换并填充作者信息
        for (Comment entity : commentEntityList) {
            CommentDTO dto = convertToDto(entity);
            User user = userMap.get(entity.getUserId());
            if (user != null) {
                dto.setUserName(user.getUserName());
            } else {
                dto.setUserName("未知");
                System.err.println("用户不存在: userId=" + entity.getUserId());
            }
            dtos.add(dto);
        }
        return dtos;
        //《论如何使用却搞不懂哈希表》
    }

    private CommentDTO convertToDto(Comment entity) {
        CommentDTO dto=new CommentDTO();
        dto.setId(entity.getId());
        dto.setArticleId(entity.getArticleId());
        dto.setUserId(entity.getUserId());
        dto.setContent(entity.getContent());
        dto.setCreatedAt(entity.getCreatedAt());
        dto.setLikes(entity.getLikes());
        return dto;
    }


    private Comment convertToEntity(CommentDTO dto) {
        Comment entity=new Comment();
        entity.setArticleId(dto.getArticleId());
        entity.setUserId(dto.getUserId());
        entity.setContent(dto.getContent());
        entity.setCreatedAt(dto.getCreatedAt());
        entity.setLikes(dto.getLikes());
        return entity;
    }











}
